<template>
	<div class="quotationPreview">
		<uni-card padding="10px 0">
			<uni-list :border="false">
				<uni-table border stripe emptyText="暂无更多数据" v-if="product.lines.length > 0">
					<!-- 表头行 -->
					<uni-tr>
						<uni-th width="100" align="center">型号</uni-th>
						<uni-th width="100" align="center">数量</uni-th>
						<uni-th width="100" align="center">销售单价</uni-th>
						<uni-th width="100" align="center">销售金额</uni-th>
					</uni-tr>
					<!-- 表格数据行 -->
					<uni-tr v-for="item in product.lines" :key="item.id">
						<uni-td align="center">{{item.name}}</uni-td>
						<uni-td align="center">
							<text>{{item.product_qty}}</text>
						</uni-td>
						<uni-td align="center">
							<text>{{item.proposed_price_unit}}</text>
						</uni-td>
						<uni-td align="center">
							<text>{{item.proposed_sale_amount}}</text>
						</uni-td>
					</uni-tr>
				</uni-table>
				<quotation-fields :fields="product.fields" :is_customized="is_customized"
					v-if="product.fields && product.fields.length > 0"></quotation-fields>
			</uni-list>
			<view class="btn" @click="toBack">返回</view>
		</uni-card>
		<safe-area></safe-area>
	</div>
</template>

<script>
	import {
		quotationPreview,
	} from "@/api/call_kw.js";
	export default {
		data() {
			return {
				id: '',
				product: {
					fields: [],
					lines: [],
				},
				// 是否定制类
				is_customized: false,
			}
		},
		onLoad(options) {
			this.id = options.id || '';
			this.getInfo();
		},
		methods: {
			getInfo() {
				quotationPreview({
					id: Number(this.id),
				}).then(res => {
					if (res.data?.result) {
						if (res.data.result.success) {
							try {
								uni.$emit('preview');
								this.is_customized = res.data?.result?.datas?.display_type_name === '定制类';
								let fields = [];
								if (res.data.result?.lines?.length > 0) {
									fields = [{
										do_type: 'readonly',
										name: "印刷类型",
										field_name: "print_type",
										value: ""
									}, {
										do_type: 'readonly',
										name: "销售价格",
										field_name: "sale_amount",
										value: ""
									}, {
										do_type: 'readonly',
										name: "让利幅度",
										field_name: "rebate_range",
										value: ""
									}, {
										do_type: 'readonly',
										name: "实际销售金额",
										field_name: "final_sale_price",
										value: ""
									}, {
										do_type: 'readonly',
										name: "订单数量",
										field_name: "quantity",
										value: ""
									}, {
										do_type: 'readonly',
										name: "客户",
										field_name: "partner_id",
										value: ""
									}, {
										do_type: 'readonly',
										name: "收货地区",
										field_name: "destination",
										value: ""
									}]
								} else {
									fields = [{
											do_type: 'readonly',
											name: "正面宽",
											field_name: "front_width",
											sequence: 1,
											value: ""
										},
										{
											do_type: 'readonly',
											name: "高",
											field_name: "height",
											sequence: 2,
											value: ""
										},
										{
											do_type: 'readonly',
											name: "侧宽",
											field_name: "side_width",
											sequence: 3,
											value: ""
										},
										{
											do_type: 'readonly',
											name: "订单数量",
											field_name: "quantity",
											sequence: 4,
											value: ""
										},
										{
											do_type: 'readonly',
											name: "含运费最低报价",
											field_name: "lowest_quotation_with_freight",
											sequence: 5,
											value: ""
										},
										{
											do_type: 'readonly',
											name: "不含运费最低报价",
											field_name: "lowest_quotation_without_freight",
											sequence: 6,
											value: ""
										},
										{
											do_type: 'readonly',
											name: "颜色数量",
											field_name: "color_quantity",
											sequence: 7,
											value: ""
										},
										{
											do_type: 'readonly',
											name: "版费",
											field_name: "plate_cost",
											sequence: 8,
											value: ""
										},
										{
											do_type: 'readonly',
											name: "含运费总金额",
											field_name: "freight_amount",
											sequence: 9,
											value: ""
										},
										{
											do_type: 'readonly',
											name: "不含运费总金额",
											field_name: "non_freight_amount",
											sequence: 10,
											value: ""
										},
										{
											do_type: 'readonly',
											name: "收货地区",
											field_name: "destination",
											sequence: 11,
											value: ""
										},
									];
								}
								fields.forEach(x => {
									x.value = res.data?.result?.order?.find(y => y.field_name === x.field_name)?.value;
								});
								// 让利幅度为0,销售价格不显示
								if (!fields?.find?.(x => x.field_name === 'rebate_range')?.value && fields?.findIndex?.(x => x
										.field_name === 'sale_amount') > -1) {
									fields?.splice?.(fields?.findIndex?.(x => x.field_name === 'sale_amount'), 1);
								}
								this.product.fields = fields.filter(x => x.value);
								this.product.lines = res.data.result?.lines.filter(x => x.find?.(x => x.field_name === 'product_qty')
									?.value > 0).map(x => {
									return {
										name: x.find?.(x => x.field_name === 'name')?.value || '',
										product_qty: x.find?.(x => x.field_name === 'product_qty')?.value || 0,
										proposed_price_unit: x.find?.(x => x.field_name === 'proposed_price_unit')?.value?.toFixed(
											2) || 0,
										proposed_sale_amount: x.find?.(x => x.field_name === 'proposed_sale_amount')?.value?.toFixed(
											0) || 0,
									}
								})
							} catch (err) {
								console.error(err);
							}
						} else {
							uni.showToast({
								icon: 'none',
								title: res.data?.result?.message || '获取失败',
							})
							setTimeout(() => {
								uni.navigateBack();
							}, 1500);
						}
					} else {
						uni.showToast({
							icon: 'none',
							title: res.data?.error?.data?.message || '获取失败',
						})
						setTimeout(() => {
							uni.navigateBack();
						}, 1500);
					}
				}).catch((e) => {
					setTimeout(() => {
						uni.navigateBack();
					}, 1500);
				})
			},
			toBack() {
				uni.navigateBack();
			},
		}
	}
</script>

<style lang="scss" scoped>
	.quotationPreview {
		width: 100%;

		/deep/.uni-card {
			margin: 0 !important;
			padding: 0 !important;
		}

		.btn {
			margin: 30rpx auto;
			width: 380rpx;
			height: 80rpx;
			border-radius: 10rpx;
			background-color: #3F7CFB;
			line-height: 80rpx;
			text-align: center;
			color: white;
		}
	}

	/deep/.quotation-fields {
		uni-list {
			.uni-list {
				flex-wrap: wrap;
				flex-direction: row;
			}
		}
	}

	/deep/.uni-table {
		min-width: unset !important;

		.uni-table-td,
		.uni-table-th {
			font-size: 24rpx !important;
		}

		.uni-table-td {
			vertical-align: middle;
		}
	}
</style>